<template>
  <center>
    <div class="app-container">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            >新增</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            >修改</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            >删除</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            >导出</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-upload
            class="upload-demo"
            action="http://localhost:8001/weigui/import"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :before-remove="beforeRemove"
            multiple
            :limit="3"
            :on-exceed="handleExceed"
            :file-list="fileList"
          >
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-col>
      </el-row>

      <el-table
        v-loading="loading"
        :data="dataList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="车牌号" align="center" prop="car_number" />
        <el-table-column label="违规种类" align="center" prop="type" />
        <el-table-column label="时间" align="center" prop="time" />
        <el-table-column label="扣除分数" align="center" prop="sub_score" />
        <el-table-column label="记录人" align="center" prop="add_people" />
        <el-table-column label="状态" align="center" prop="is_delete"
          ><template slot-scope="scope"
            ><el-switch
              v-model="scope.row.is_delete"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-value="1"
              inactive-value="0"
              active-text="存在"
              inactive-text="删除"
            >
            </el-switch></template
        ></el-table-column>

        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryParams.pageNumber"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="queryParams.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="this.total"
      >
      </el-pagination>

      <!-- 添加或修改参数配置对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="500px">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="车牌号" prop="car_number">
            <el-input v-model="form.car_number" placeholder="请输入车牌号" />
          </el-form-item>
          <el-form-item label="违规种类" prop="type">
            <el-input v-model="form.type" placeholder="请输入违规种类" />
          </el-form-item>
          <el-form-item label="扣除分数" prop="sub_score">
            <el-input v-model="form.sub_score" placeholder="请输入扣除分数" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </center>
</template>

<script>
import { getWeigui } from "@/api/weigui";
import { addData } from "@/api/weigui";
import { updateData } from "@/api/weigui";
import { getDatabyid } from "@/api/weigui";
import { delData } from "@/api/weigui";
import { exportData } from "@/api/weigui";
export default {
  name: "Data",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 字典表格数据
      dataList: [],
      // 默认字典类型
      defaultDictType: "",
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态数据字典
      statusOptions: [],
      // 类型数据字典
      typeOptions: [],
      // 查询参数
      queryParams: {
        pageNumber: 1,
        pageSize: 10,
        dictName: undefined,
        car_number: undefined,
        status: undefined
      },
      // 表单参数
      form: {
        car_number: "",
        type: "",
        sub_score: "",
        id: ""
      },
      // 表单校验
      rules: {
        car_number: [
          { required: true, message: "数据标签不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "数据键值不能为空", trigger: "blur" }
        ],
        sub_score: [
          { required: true, message: "数据顺序不能为空", trigger: "blur" }
        ]
      },

      fileList: []
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //上传文件
    handleRemove(file, fileList) {
      console.log(file, fileList);
      this.$message(file.response.data);
    },
    handlePreview(file) {
      console.log(file);
    },
    handleExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 3 个文件，本次选择了 ${
          files.length
        } 个文件，共选择了 ${files.length + fileList.length} 个文件`
      );
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`);
    },

    //分页点击方法
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.queryParams.pageSize = val;
      var params = {
        pageNumber: this.queryParams.pageNumber,
        pageSize: this.queryParams.pageSize
      };
      this.getList(params);
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.queryParams.pageNumber = val;
      var params = {
        pageNumber: this.queryParams.pageNumber,
        pageSize: this.queryParams.pageSize
      };
      this.getList(params);
    },

    /** 查询违规数据列表 */
    getList() {
      this.loading = true;
      getWeigui("/weigui/all", this.queryParams).then(response => {
        if (response.code === 200) {
          this.dataList = response.data.list;
          this.total = response.data.totalRow;
          this.loading = false;
        } else if (res.code === 500) {
          this.$message(response.data);
        }
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        car_number: undefined,
        type: undefined,
        add_people: undefined
      };
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNumber = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true;
      this.title = "添加违规记录";
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.form.car_number = row.car_number;
      this.form.sub_score = row.sub_score;
      this.form.type = row.type;
      this.form.id = row.id;
      this.open = true;
      this.title = "修改字典数据";
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          addData("/weigui/add", this.form).then(response => {
            if (response.code === 200) {
              this.$message(response.data);
              this.open = false;
              this.getList();
            } else {
              this.$message(response.data);
            }
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id || this.ids;
      this.$confirm('是否确认删除字典编码为"' + id + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return delData("/weigui/delete", { id: id });
        })
        .then(() => {
          this.$message("删除成功");
          this.getList();
        })
        .catch(function() {});
    },

    /** 导出按钮操作 */
    handleExport() {
      this.$confirm("是否确认导出所有数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return exportData("/weigui/export");
        })
        .then(response => {
          // console.log(response);
          // this.download(response);
          this.$message("导出成功");
        })
        .catch(function() {});
    }
  }
};
</script>

<style scoped>
.app-container {
  margin-top: 10px;
}
</style>
